package com.hjh.mybatisxml.test;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjh.mybatisxml.entity.User;
import com.hjh.mybatisxml.mapper.BaseMapper;
import com.hjh.mybatisxml.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
public class UserMapperTester {
    @Resource
    private UserMapper userMapper;
    @Resource
    private BaseMapper baseMapper;

//    @Test
//    public void testInsert() throws Exception{
//        userMapper.insert(new User("hjh","123456",8L,1));
//    }
//
//    @Test
//    public void testGet(){
//        User user = userMapper.get(8L);
//        System.out.println("usrName:" + user.getUsrName());
//    }

    @Test
    public void testSelectById(){
        User user = userMapper.selectById(8L);
        System.out.println("usrName:" + user.getUsrName());
    }

    @Test
    public void testInsert(){
        baseMapper.insert(new User("MyBatis-Plus","123456",8L,1));
    }

    @Test
    public void testFindAll(){
        List<User> list = baseMapper.selectList(null);
        for (User user:list) {
            System.out.println("usrName:" + user.getUsrName());
        }
    }

    @Test
    public void testFind(){//根据用户名和密码查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_name","MyBatis-Plus");
        wrapper.eq("usr_password","123456");
        List<User> list = baseMapper.selectList(wrapper);
        if (list != null){
            for (User user:list) {
                System.out.println("usrId:" + user.getUsrId());
            }
        }
    }

    @Test
    public void testPagination(){//分页查询
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_role_id",2L);//查询条件：角色Id为2
        //分页对象 ： 查询获得User对象，查看第1页数据，每页显示2条记录
        Page<User> page = new Page<User>(1,2);
        IPage<User> userPager = baseMapper.selectPage(page,wrapper);
        System.out.println("总记录数---- -->" + userPager.getTotal());
        System.out.println("总页数---- -->" + userPager.getPages());
        System.out.println("当前页数---- -->" + userPager.getCurrent());
        System.out.println("每页记录数---- -->" + userPager.getSize());
        System.out.println("当前记录---- -->");
        for (User user:userPager.getRecords()) {
            System.out.println("usrId:" + user.getUsrId());
        }
    }
}
